package com.hxzy.controller.storetype;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.util.BeanValidatorUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.common.ResultAjax;
import com.hxzy.entity.StoreType;
import com.hxzy.service.StoreTypeServie;
import com.hxzy.service.impl.StoreTypeServieImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/3/8 22:01
 */
@WebServlet(urlPatterns = "/Add/storetype")
public class AddStoreTypeServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        try {
            StoreType storeType = super.CollectiongParameterToJSON(req, StoreType.class);
            Set<String> validator = BeanValidatorUtil.validator(StoreType.class, storeType);
            if (validator.isEmpty()){
                StoreTypeServie servie=new StoreTypeServieImpl();
                int insert = servie.insert(storeType);
                ResultAjax resultAjax;
                if (insert>0){
                    resultAjax=ResultAjax.success("插入成功");
                }else {
                    resultAjax=ResultAjax.error("插入失败");
                }
                super.render(resp, JSONObject.toJSONString(resultAjax));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
